package com.qiqijuice.sales;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.db4o.ObjectContainer;
import com.qiqijuice.sales.Order.OrderState;

public class OrderView extends Composite {
	
	OperationsView ov;
	OrderListView olv;
	ProductOrderView prv;
	OrderItemsView oiv;
	
	OrderView() {
		this(null);
	}
	
	OrderView(Composite frame) {
		super(frame, SWT.NONE);
		
		MigLayout layout = new MigLayout("fill", "");
		setLayout(layout);
		
		ov = new OperationsView(this);
		ov.setLayoutData("east");
		ov.addListener(OperationsView.EVT_NEW_ORDER, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				createNewOrder();
				olv.updateChoices();
				oiv.viewOrder(olv.getSelectedOrder());
			}
		});
		
		ov.addListener(OperationsView.EVT_DEL_ORDER, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				deleteOrder();
				olv.updateChoices();
				oiv.viewOrder(olv.getSelectedOrder());
			}
		});

		ov.addListener(OperationsView.EVT_CLOSE_ORDER, new Listener() {
			@Override
			public void handleEvent(Event event) {
				closeOrder();
				olv.updateChoices();
				oiv.viewOrder(olv.getSelectedOrder());
			}
		});
		
		olv = new OrderListView(this);
		olv.setLayoutData("north, height 100");
		olv.addListener(OrderListView.EVT_ORDER_SELETED, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				// TODO Auto-generated method stub
				oiv.viewOrder((Order) event.data);
			}
		});
		
		oiv = new OrderItemsView(this);
		oiv.setLayoutData("west, width 200:40%:n");
		oiv.addListener(OrderItemsView.EVT_MODIFY_ORDER, new Listener() {

			@Override
			public void handleEvent(Event event) {
				modifyOrderItem((OrderItem) event.data);
				oiv.updateItems();
			}
			
		});
		oiv.addListener(OrderItemsView.EVT_DELETE_ITEM, new Listener() {
			@Override
			public void handleEvent(Event event) {
				markOrderItemRemoved((OrderItem) event.data);
				oiv.updateItems();
			}
		});
		prv = new ProductOrderView(this);
		prv.setLayoutData("center, grow");
		prv.addListener(ProductOrderView.EVT_ADD_PRODUCT, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				addOrderItem((Product)event.data);
				oiv.updateItems();
			}
		});
	}
	protected void markOrderItemRemoved(OrderItem data) {
		if (data != null) {
			data.setRemoved(true);
			ObjectContainer oc = Db4oContainerFactory.getDB();
			oc.store(data);
		}
	}

	protected void addOrderItem(Product data) {
		OrderItemDetailsDialog dlg = new OrderItemDetailsDialog(getShell().getDisplay());
		if (dlg.open()) {
			float quantity = dlg.getQuantity();
			OrderItem oi = new OrderItem(data, quantity);
			Order o = olv.getSelectedOrder();
			if (o != null) {
				o.addOrderItem(oi);
				ObjectContainer oc = Db4oContainerFactory.getDB();
				oc.store(o);	
			}
		}
	}

	protected void modifyOrderItem(OrderItem i) {
		Order order = olv.getSelectedOrder();
		if (order != null) {
			OrderItemDetailsDialog dlg = new OrderItemDetailsDialog(getShell().getDisplay());
			if (dlg.open()) {
				float quantity = dlg.getQuantity();
				i.setQuantity(quantity);
				ObjectContainer oc = Db4oContainerFactory.getDB();
				oc.store(i);
			}
		}
		
	}
	
	protected void closeOrder() {
		Order order = olv.getSelectedOrder();
		if (order != null) {
			order.setState(OrderState.PAYED);
			ObjectContainer oc = Db4oContainerFactory.getDB();
			oc.store(order);
		}
	}

	protected void deleteOrder() {
		Order order = olv.getSelectedOrder();
		if (order != null) {
			ObjectContainer oc = Db4oContainerFactory.getDB();
			order.setState(OrderState.REMOVED);
			oc.store(order);
		}		
	}

	protected void createNewOrder() {
		Order order = new Order();
		ObjectContainer oc = Db4oContainerFactory.getDB();
		oc.store(order);
		
		olv.setSelectedOrder(order);
	}
	
	
}
